package com.psa.element;

import android.content.Context;
import android.database.Cursor;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import com.psa.adapter.SqlObjectListAdapter;
import com.psa.system.R;
import com.psa.tool.CreateSqlTools;
import com.psa.tool.DBTool;
import com.psa.tool.SystemStatus;
import com.wxw.BaseLibrary.adapter.IElementListAdapter;
import com.wxw.BaseLibrary.element.IListViewElement;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author david
 * @date 2014-06-17
 */
public class CustomerEditorElement implements IListViewElement {
    private Field field = null;
    private Customer customer = null;
    private Column column = null;
    private View view = null;
    private boolean isEditable = false;
    private List<ISqlObject> iSqlObjects = null;
    private Context context = null;

    public CustomerEditorElement(Customer customer, Field field, Context context){
        this.customer = customer;
        this.field = field;
        column = field.getAnnotation(Column.class);
        this.context = context;
    }

    public int index(){
        return column!=null?column.index():-1;
    }

    public void setEditable(boolean editable){
        isEditable = editable;
    }

    public boolean isEditable(){
        return isEditable;
    }

    @Override
    public View createView(IElementListAdapter adapter) {
        if (isEditable) {
            switch (column.inputType()) {
                case 0:
                    view = View.inflate(this.context, R.layout.list_item_input, null);
//                    ((EditText)view.findViewById(R.id.et_list_item_input)).addTextChangedListener(new TextWatcher() {
//                        @Override
//                        public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
//
//                        }
//
//                        @Override
//                        public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
//                            field.setAccessible(true);
//                            try {
//                                field.set(customer, charSequence.toString());
//                            } catch (Exception e) {
//                                e.printStackTrace();
//                                Log.e(SystemStatus.TAG, e.getMessage());
//                            }
//                        }
//
//                        @Override
//                        public void afterTextChanged(Editable editable) {
//
//                        }
//                    });
                    break;
                case 1:
                    view = View.inflate(this.context, R.layout.list_item_spinner, null);
                    fillSpinner((Spinner) view.findViewById(R.id.sp_list_item_input));
                    ((Spinner) view.findViewById(R.id.sp_list_item_input)).setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                        @Override
                        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                            field.setAccessible(true);
                            try {
                                field.set(customer, iSqlObjects.get(i).toString());
                            } catch (Exception e) {
                                e.printStackTrace();
                                Log.e(SystemStatus.TAG, e.getMessage());
                            }
                        }

                        @Override
                        public void onNothingSelected(AdapterView<?> adapterView) {

                        }
                    });
                    break;
                default:
                    view = View.inflate(this.context, R.layout.list_item_view, null);
                    break;
            }
        } else {
            view = View.inflate(this.context, R.layout.list_item_view, null);
        }
        return view;
    }

    @Override
    public View getView() {
        return view;
    }

    @Override
    public View setViewContent(View view) {
        if (!this.view.equals(view)) {
            view = this.view;
        }
        if (isEditable) {
            switch (column.inputType()) {
                case 0:
                    ((TextView)view.findViewById(R.id.tv_list_item_input)).setText(column.caption()!=""?column.caption():column.name());
                    ((EditText)view.findViewById(R.id.et_list_item_input)).setText(CreateSqlTools.getFieldValue(customer, field));
                    break;
                case 1:
                    ((TextView)view.findViewById(R.id.tv_list_item_input)).setText(column.caption()!=""?column.caption():column.name());
                    setSelection((Spinner) view.findViewById(R.id.sp_list_item_input));
                    break;
                default:
                    ((TextView)view.findViewById(R.id.tv_list_item_key)).setText(column.caption()!=""?column.caption():column.name());
                    ((TextView)view.findViewById(R.id.tv_list_item_value)).setText(CreateSqlTools.getFieldValue(customer, field));
                    break;
            }
        } else {
            ((TextView)view.findViewById(R.id.tv_list_item_key)).setText(column.caption()!=""?column.caption():column.name());
            ((TextView)view.findViewById(R.id.tv_list_item_value)).setText(CreateSqlTools.getFieldValue(customer, field));
        }
        return view;
    }

    private void fillSpinner(Spinner spinner){
        iSqlObjects = new ArrayList<ISqlObject>();
        Cursor cursor = null;
        int i = 0, index = 0;
        if (column.name().equals("BusinessType")) {
            cursor = DBTool.getInstance().query("Select * From Channel");
            Channel channel = new Channel();
            while ((channel = (Channel) channel.full(cursor)) != null) {
                if (channel.ID.equals(customer.getBusinessType())) {
                    index = i;
                }
                i++;
                iSqlObjects.add(channel);
            }
        } else if (column.name().equals("ManagedDepartment")) {
            cursor = DBTool.getInstance().query("Select * From Department");
            Department department = new Department();
            while ((department = (Department) department.full(cursor)) != null) {
                if (department.ID.equals(customer.getManagedDepartment())) {
                    index = i;
                }
                i++;
                iSqlObjects.add(department);
            }
        } else if (column.name().equals("DistrictID")) {
            cursor = DBTool.getInstance().query("Select * From District");
            District district = new District();
            while ((district = (District) district.full(cursor)) != null) {
                if (district.ID.equals(customer.getDistrictID())) {
                    index = i;
                }
                i++;
                iSqlObjects.add(district);
            }
        } else {
            cursor = DBTool.getInstance().query("Select * From TradeMode");
            TradeMode tradeMode = new TradeMode();
            while ((tradeMode = (TradeMode) tradeMode.full(cursor)) != null) {
                if (tradeMode.ID.equals(customer.getTradeMode())) {
                    index = i;
                }
                i++;
                iSqlObjects.add(tradeMode);
            }
        }
        if (cursor!=null) {
            cursor.close();
        }
        SqlObjectListAdapter adapter = new SqlObjectListAdapter(view.getContext(), iSqlObjects);
        spinner.setAdapter(adapter);
        spinner.setSelection(index);
    }

    private void setSelection(Spinner spinner){
        int index = 0;
        if (column.name().equals("BusinessType")) {
            for (int i =0; i<iSqlObjects.size();i++){
                if (((Channel)iSqlObjects.get(i)).ID.equals(customer.getBusinessType())) {
                    index = i;
                    break;
                }
            }
        } else if (column.name().equals("ManagedDepartment")) {
            for (int i =0; i<iSqlObjects.size();i++){
                if (((Department)iSqlObjects.get(i)).ID.equals(customer.getManagedDepartment())) {
                    index = i;
                    break;
                }
            }
        } else if (column.name().equals("DistrictID")) {
            for (int i =0; i<iSqlObjects.size();i++){
                if (((District)iSqlObjects.get(i)).ID.equals(customer.getDistrictID())) {
                    index = i;
                    break;
                }
            }
        } else {
            for (int i =0; i<iSqlObjects.size();i++){
                if (((TradeMode)iSqlObjects.get(i)).ID.equals(customer.getTradeMode())) {
                    index = i;
                    break;
                }
            }
        }
        spinner.setSelection(index);
    }
}
